package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.MaxMinNumber;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.ScoreTrend;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MonthBillActivity extends BaseActivity implements View.OnClickListener, ScoreTrend.ScoreTrendClick {

    private static final String TAG = "MonthBillActivity";
    private ScoreTrend scoreTrend, scoreTrend2;
    private String[] monthText = new String[12];
    private String[] monthText2 = new String[12];
    private List<Double> score = new ArrayList<>();
    private List<Double> score2 = new ArrayList<>();
    private List<String> times = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month_bill);
        initTitle();

//        Random random = new Random();
//        for (int i = 0; i < monthText.length; i++) {
//            double b = Math.abs(random.nextDouble()*100);
//            Log.e(TAG, "onCreate: "+b );
//            score.add(b);
//        }
        scoreTrend = (ScoreTrend) findViewById(R.id.scoreTrend);
        scoreTrend2 = (ScoreTrend) findViewById(R.id.scoreTrend2);
//        setScoreTrendData(monthText, score);
        upLoad();
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("月账单");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }

    private void setScoreTrendData(String[] monthText, List<Double> score, ScoreTrend scoreTrend) {

        MaxMinNumber number = getMaxMinNumber(score);
        scoreTrend.setMonthText(monthText);
        scoreTrend.setScore(score);
        scoreTrend.setTrendClick(this);
        scoreTrend.setMaxScore((int) number.getMaxNumbrt());
        scoreTrend.setMinScore((int) number.getMinNumber());
        scoreTrend.refult();
    }

    private MaxMinNumber getMaxMinNumber(List<Double> num) {
        MaxMinNumber number = new MaxMinNumber();
        double max;
        double min;
        if (num.size() > 2) {
            max = num.get(0);
            min = num.get(0);
            for (int i = 0; i < num.size(); i++) {
                if (max < num.get(i)) {
                    max = num.get(i);
                }
                if (min > num.get(i)) {
                    min = num.get(i);
                }

            }
        } else if (num.size() == 1) {
            max = num.get(0);
            min = num.get(0);
        } else {
            max = 0;
            min = 0;
        }

        number.setMaxNumbrt(max);
        number.setMinNumber(min);
        return number;
    }

    @Override
    public void onClick(int index) {

    }

    private void upLoad() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.MONEY_MONTHLOGLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                Log.e("----", result);
                try {
                    JSONObject res = new JSONObject(result);
                    if ("1".equals(res.optString("result"))) {
                        JSONObject shouru = res.optJSONObject("shouru");
                        JSONObject zhichu = res.optJSONObject("zhichu");
                        for (int i = 0; i < 12; i++) {
                            // Log.e("----111", shouru.optString("" + i));

                            monthText[i] = "" + i;
                            if (shouru.optString("" + i) == null || "".equals(shouru.optString("" + i))) {
                                score.add(Double.parseDouble("0"));
                            } else {
                                score.add(Double.parseDouble(shouru.optString("" + i)));
                            }
                        }
                        for (int i = 0; i < 12; i++) {
                            //Log.e("----111", shouru.optString("" + i));

                            monthText2[i] = "" + i;
                            if (zhichu.optString("" + i) == null || "".equals(zhichu.optString("" + i))) {
                                score2.add(Double.parseDouble("0"));
                            } else {
                                score2.add(Double.parseDouble(zhichu.optString("" + i)));
                            }
                        }
                        //monthText=

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                setScoreTrendData(monthText, score, scoreTrend);
                setScoreTrendData(monthText2, score2, scoreTrend2);
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }
}
